package connect4swt.gui;

import com.swtdesigner.*;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;


import connect4swt.ClientApplication;
import connect4client.core.Controller;
import connect4client.net.ConnectionController;
import connect4client.net.servers.Server;

/**
 * @author  Joel Cordeiro
 */
public class LoginWindow {

	public Object open() {
		final Display display = Display.getDefault();

		createContents();
		shell.open();
		shell.layout();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}


	protected void createContents() {
		shell = new Shell(SWT.DIALOG_TRIM);
		shell.setImage(SWTResourceManager.getImage(LoginWindow.class, "/images/connect4Icon.ico"));
		shell.setSize(302, 254);
		shell.setText("Login");

		final Label lblServer = new Label(shell, SWT.RIGHT);
		lblServer.setBounds(18, 37, 60, 16);
		lblServer.setText("Server:");

		cmbServer = new Combo(shell, SWT.NONE);
		cmbServer.setBounds(83, 32, 209, 30);
		cmbServer.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				cmbIP.removeAll();
				setCmbIP(cmbServer.getText());
			}
		});

		final Label lblIP = new Label(shell, SWT.RIGHT);
		lblIP.setBounds(15, 62, 63, 16);
		lblIP.setText("IP:");

		cmbIP = new Combo(shell, SWT.NONE);
		cmbIP.setBounds(83, 62, 209, 30);

		final Label lblLogin = new Label(shell, SWT.RIGHT);
		lblLogin.setText("Login:");
		lblLogin.setBounds(18, 108, 60, 16);

		final Label lblPassword = new Label(shell, SWT.RIGHT);
		lblPassword.setBounds(15, 139, 63, 16);
		lblPassword.setText("Password:");

		txtLogin = new Text(shell, SWT.BORDER);
		txtLogin.setBounds(83, 102, 209, 21);

		txtPassword = new Text(shell, SWT.BORDER);
		txtPassword.setEchoChar('*');
		txtPassword.setBounds(83, 133, 209, 21);

		final Button btnOK = new Button(shell, SWT.NONE);
		shell.setDefaultButton(btnOK);
		btnOK.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {

				if (txtLogin.getText().length() == 0 || cmbIP.getText() == null) {
					MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
					msgBox.setText("Warning");
					msgBox.setMessage("Please insert Login/Password!");
					msgBox.open();
					return;
				} else
					autentication();
			}
		});
		btnOK.setText("OK");
		btnOK.setBounds(156, 181, 60, 28);

		final Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {
				shell.dispose();
				System.exit(0);
			}
		});
		btnCancel.setBounds(222, 181, 70, 28);
		btnCancel.setText("Cancel");

		final Label lblHeader = new Label(shell, SWT.NONE);
		lblHeader.setImage(SWTResourceManager.getImage(LoginWindow.class, "/images/log.png"));
		lblHeader.setBounds(0, 0, 300, 26);

		final Link lnkRegister = new Link(shell, SWT.NONE);
		lnkRegister.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent arg0) {
				RegisterWindow registerWindow = RegisterWindow.getInstance();
				registerWindow.open();
			}
		});
		lnkRegister.setText("<a href=\"http://www.eclipse.org\">Register...</a>");
		lnkRegister.setBounds(3, 183, 75, 16);

		initializeFields();
	}


	private void setCmbIP(String type) {

		if (type.equals("Socket")) {
			for (String text : socketServers) {
				cmbIP.add(text);
			}
		} else if (type.equals("RMI")) {
			for (String text : rmiServers) {
				cmbIP.add(text);
			}
		}
		cmbIP.select(0);
	}


	public void autentication() {
		Server server = null;
		ConnectionController.CommunicationMethod method = null;
		if (cmbServer.getText().equals("RMI")) {
			server = new Server(cmbIP.getText(), 1099);
			method = ConnectionController.CommunicationMethod.RMI;
		} else if (cmbServer.getText().equals("Socket")) {
			server = new Server(cmbIP.getText(), 32000);
			method = ConnectionController.CommunicationMethod.TCP;
		}

		Controller controller = ClientApplication.getController();
		controller.getGUIMediator().sendLoginMessage(method, server, txtLogin.getText(), txtPassword.getText());
	}


	public void showConnectionFailed() {
		shell.getDisplay().syncExec(new Runnable() {
			public void run() {
				MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
				msgBox.setText("Warning");
				msgBox.setMessage("Connection failed!");
				msgBox.open();
			}
		});
	}


	private void initializeFields() {

		cmbServer.add("Socket");
		cmbServer.add("RMI");
		cmbServer.select(1);

		this.socketServers = new Vector<String>();
		this.socketServers.add("127.0.0.1");
		this.socketServers.add("192.168.1.4");

		this.rmiServers = new Vector<String>();
		this.rmiServers.add("127.0.0.1");
		this.rmiServers.add("192.168.1.3");

		setCmbIP(cmbServer.getText());
	}


	public void showRegistrationSucessfull() {
		shell.getDisplay().syncExec(new Runnable() {
			public void run() {
				MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
				msgBox.setText("Information");
				msgBox.setMessage("Registration Sucessfull!");
				msgBox.open();
			}
		});
	}


	


	public void showLoginFailed() {
		shell.getDisplay().syncExec(new Runnable() {
			public void run() {
				MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
				msgBox.setText("Warning");
				msgBox.setMessage("Login failed!");
				msgBox.open();
			}
		});
	}


	public void dispose() {
		if (!isDisposed()) {
			shell.getDisplay().syncExec(new Runnable() {
				public void run() {
					shell.dispose();
				}
			});
		}
	}


	public boolean isDisposed() {
		if (shell == null)
			return true;

		return shell.isDisposed();

	}


	/**
	 * @return
	 * @uml.property  name="instance"
	 */
	public static LoginWindow getInstance() {
		return instance;
	}


	private LoginWindow() {
	}


	private Vector<String> rmiServers;
	private Vector<String> socketServers;

	private static LoginWindow instance = new LoginWindow();
	private Combo cmbIP;
	private Combo cmbServer;
	private Text txtLogin;
	private Text txtPassword;
	private Object result;
	private Shell shell;
}
